package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.restaurant.*;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class RestaurantBaseData {
    private static Logger logger = LoggerFactory.getLogger(RestaurantBaseData.class);
    private static ConcurrentHashMap<Integer, RestaurantTask> restaurantTaskMap = new ConcurrentHashMap<>(); // 餐厅任务配置表
    private static ConcurrentHashMap<Integer, RestaurantMain> restaurantMainMap = new ConcurrentHashMap<>(); // 餐厅配置表
    private static ConcurrentHashMap<Integer, RestaurantFood> restaurantFoodMap = new ConcurrentHashMap<>(); // 餐厅美食攻略配置表
    private static ConcurrentHashMap<Integer, RestaurantGuest> restaurantGuestMap = new ConcurrentHashMap<>(); // 餐厅客人配置表
    private static ConcurrentHashMap<Integer, RestaurantUpgrade> restaurantUpgradeMap = new ConcurrentHashMap<>(); // 餐厅升级配置表
    private static ConcurrentHashMap<Integer, RestaurantReception> restaurantReceptionMap = new ConcurrentHashMap<>(); // 餐厅模块接待次数上限配置表



    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initRestaurantMain());
        futures.add(initRestaurantFood());
        futures.add(initRestaurantGuest());
        futures.add(initRestaurantTask());
        futures.add(initRestaurantUpgrade());
        futures.add(initRestaurantReception());
    }


    /**
     * 初始化餐厅任务配置表
     */
    public static Future<Void> initRestaurantTask() {
        String table = "restaurant_task";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, RestaurantTask> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        RestaurantTask entity = new RestaurantTask(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    restaurantTaskMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static RestaurantTask getRestaurantTask(int id) {
        RestaurantTask result = restaurantTaskMap.get(id);
        if (result == null) {
            logger.error("getRestaurantTask error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, RestaurantTask> getRestaurantTaskMap() { return restaurantTaskMap; }


    /**
     * 初始化餐厅配置表
     */
    public static Future<Void> initRestaurantMain() {
        String table = "restaurant_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, RestaurantMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        RestaurantMain entity = new RestaurantMain(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    restaurantMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static RestaurantMain getRestaurantMain(int id) {
        RestaurantMain result = restaurantMainMap.get(id);
        if (result == null) {
            logger.error("getRestaurantMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, RestaurantMain> getRestaurantMainMap() { return restaurantMainMap; }

    /**
     * 初始化餐厅美食攻略配置表
     */
    public static Future<Void> initRestaurantFood() {
        String table = "restaurant_food";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, RestaurantFood> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        RestaurantFood entity = new RestaurantFood(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    restaurantFoodMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static RestaurantFood getRestaurantFood(int id) {
        RestaurantFood result = restaurantFoodMap.get(id);
        if (result == null) {
            logger.error("getRestaurantFood error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, RestaurantFood> getRestaurantFoodMap() { return restaurantFoodMap; }

    /**
     * 初始化餐厅客人配置表
     */
    public static Future<Void> initRestaurantGuest() {
        String table = "restaurant_guest";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, RestaurantGuest> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        RestaurantGuest entity = new RestaurantGuest(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    restaurantGuestMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static RestaurantGuest getRestaurantGuest(int id) {
        RestaurantGuest result = restaurantGuestMap.get(id);
        if (result == null) {
            logger.error("getRestaurantGuest error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, RestaurantGuest> getRestaurantGuestMap() { return restaurantGuestMap; }

    /**
     * 初始化餐厅升级配置表
     */
    public static Future<Void> initRestaurantUpgrade() {
        String table = "restaurant_upgrade";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, RestaurantUpgrade> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        RestaurantUpgrade entity = new RestaurantUpgrade(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    restaurantUpgradeMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static RestaurantUpgrade getRestaurantUpgrade(int id) {
        RestaurantUpgrade result = restaurantUpgradeMap.get(id);
        if (result == null) {
            logger.error("getRestaurantUpgrade error key = " + id);
        }
        return result;
    }

    public static RestaurantUpgrade getRestaurantUpgrade(int restaurantId, int level) {
        for (RestaurantUpgrade entity : restaurantUpgradeMap.values()) {
            if (entity.getRestaurantId() == restaurantId && entity.getLevel() == level) {
                return entity;
            }
        }
        return null;
    }
    public static ConcurrentHashMap<Integer, RestaurantUpgrade> getRestaurantUpgradeMap() { return restaurantUpgradeMap; }


    /**
     * 初始化餐厅模块接待次数上限配置表
     */
    public static Future<Void> initRestaurantReception() {
        String table = "restaurant_reception";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, RestaurantReception> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        RestaurantReception entity = new RestaurantReception(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    restaurantReceptionMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static RestaurantReception getRestaurantReception(int id) {
        RestaurantReception result = restaurantReceptionMap.get(id);
        if (result == null) {
            logger.error("getRestaurantReception error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, RestaurantReception> getRestaurantReceptionMap() { return restaurantReceptionMap; }

    // 根据人气值获取接待次数的上限
    public static int getReceptionMaxByScore(int score) {
        int max = 0;
        for (RestaurantReception restaurantReception : restaurantReceptionMap.values()) {
            if (score >= restaurantReception.getScore() && restaurantReception.getReceptionNum() > max) {
                max = restaurantReception.getReceptionNum();
            }
        }
        return max;
    }
}
